package cn.kooki.app.duobao.ui.Activity.Goods;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.User.record.RecordItem;
import cn.kooki.app.duobao.data.Bean.base.BaseResponse;
import cn.kooki.app.duobao.data.Bean.detail.CodeItem;
import cn.kooki.app.duobao.data.Bean.detail.CodeResponse;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.ui.Activity.Comm.RootAcitivity;
import cn.kooki.app.duobao.ui.Adapter.CodeListAdapter;
import cn.kooki.app.duobao.ui.widget.ActionBarMenu;
import cn.kooki.app.duobao.util.ScreenUtils;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.manager.AppManager;
import cn.kooki.app.duobao.util.manager.ShopCartManager;
import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/29.
 */
public class OrderCodeListActivity extends BaseNavActivity implements View.OnClickListener {
    @Bind(R.id.tv_mycode_goodsname)
    TextView tvMycodeGoodsname;
    @Bind(R.id.mycode_list)
    TextView mycodeList;
    @Bind(R.id.tv_mycode_goodsjoins)
    TextView tvMycodeGoodsjoins;
    @Bind(R.id.fragment_code_list)
    ListView fragmentCodeList;
    private RecordItem recordItem;
    private String uid;
    private CodeListAdapter adapter;
    private ActionBarMenu barMenu;

    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.activity_order_code_list);
        ButterKnife.bind(this);


    }

    private ArrayList<CodeItem> datas = new ArrayList<>();

    @Override
    protected void afterView() {
        int size = ShopCartManager.getInstance().getCartsSize();
        if (size > 0) {
            topRefresh.setImageResource(R.drawable.ic_menu_overflow_withnew);
        } else {
            topRefresh.setImageResource(R.drawable.ic_menu_overflow);
        }
        topRefresh.setVisibility(View.VISIBLE);
        topRefresh.setOnClickListener(this);
        recordItem = getIntent().getParcelableExtra("item");
        tvMycodeGoodsname.setText(recordItem.title);
        uid = getIntent().getStringExtra("uid");
        mycodeList.setText("期号 : " + recordItem.qishu);
        setTitle(R.string.activity_order_code_list);
        SpannableString jonin = new SpannableString("本期参与了" + recordItem.q_num + "人次");
        jonin.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.main_red)), 5, jonin.length() - 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvMycodeGoodsjoins.setText(jonin);

        adapter = new CodeListAdapter(this, datas, true);
        fragmentCodeList.setAdapter(adapter);

        MainFactory.getApiInstance().getRecordCordList(uid, recordItem.id, recordItem.qishu, new Callback<CodeResponse>() {
            @Override
            public void success(CodeResponse baseResponse, Response response) {
                if (baseResponse.data != null) {
                    datas.addAll(baseResponse.data);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });

        fragmentCodeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CodeItem item = datas.get(position);
                Intent intent = new Intent(OrderCodeListActivity.this, MyCodeAcitvity.class);
                intent.putExtra("code", item.goucode);
                intent.putExtra("item", recordItem);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onClick(View v) {
        if (v == topRefresh) {
            showMenu();
        }
    }
    private void showMenu() {
        if (barMenu == null) {
            barMenu = new ActionBarMenu(this);
            barMenu.setActionMenuListener(new ActionBarMenu.ActionBarMenuClickListener() {
                @Override
                public void onItemClick(int index) {
                    ChangePageEvent pageEvent = new ChangePageEvent();
                    if (index == 0) {
                        pageEvent.item = 3;
                    } else {
                        pageEvent.item = 0;
                    }
                    EventBus.getDefault().post(pageEvent);
                    AppManager.getAppManager().getTopWith(RootAcitivity.class);
                }
            });
        }
        barMenu.showAsDropDown(topRefresh, -ScreenUtils.dpToPxInt(this, 80), 0);
    }
}
